package com.xyw.code.platform.webapps.rest;


import com.xyw.code.common.core.entity.vo.Result;
import com.xyw.code.platform.common.entity.dto.RoleDTO;
import com.xyw.code.platform.common.entity.enums.CommonStatusEnum;
import com.xyw.code.platform.common.entity.po.SysRole;
import com.xyw.code.platform.webapps.service.ISysRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 系统角色表 前端控制器
 * </p>
 *
 * @author xyw
 * @since 2020-02-07
 */
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {

    @Resource
    private ISysRoleService roleService;

    /**
     * 获取角色列表
     *
     * @return
     */
    @GetMapping("/query")
    @ApiOperation(value = "获取角色列表",notes = "获取角色列表")
    public Result query(@RequestParam(required = false) String roleName, @RequestParam(required = false) String deptId) {
        return Result.success(roleService.selectRoleList(roleName,deptId));
    }

    /**
     * 保存角色及权限
     *
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增角色",notes = "新增角色")
    public Result add(@RequestBody RoleDTO roleDTO) {
        return Result.success(roleService.saveRoleMenu(roleDTO));
    }

    /**
     * 更新角色
     *
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新角色",notes = "更新角色")
    public Result update(@RequestBody RoleDTO roleDTO) {
        return Result.success(roleService.updateRoleMenu(roleDTO));
    }

    /**
     * 删除角色
     *
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除角色",notes = "删除角色")
    public Result delete(@PathVariable String id) {
        SysRole role = new SysRole();
        role.setId(id);
        role.setDelFlag(CommonStatusEnum.DELETE.getType());
        return Result.success(roleService.updateById(role));
    }


    /**
     * 获取部门下拉框
     * @return
     */
    @GetMapping("/getDsTypes/{deptId}")
    public Result getDsTypes(@PathVariable String deptId) {
        return Result.success(roleService.getDsTypes(deptId));
    }

    /**
     * 根据角色id获取菜单
     *
     * @param roleId
     * @return
     */
    @GetMapping("/getRoleMenus/{roleId}")
    public Result getRoleMenus(@PathVariable("roleId") String roleId) {
        return Result.success(roleService.findMenuListByRoleId(roleId));
    }
}

